package com.hidt.shop.controller;

import com.github.pagehelper.PageInfo;
import com.hidt.shop.domain.Cart;
import com.hidt.shop.domain.Order_item;
import com.hidt.shop.domain.Order_table;
import com.hidt.shop.service.CartService;
import com.hidt.shop.service.Order_itemService;
import com.hidt.shop.service.Order_tableService;
import com.hidt.shop.util.IDUtil;
import com.hidt.shop.util.RestDate;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fhy
 * @Title Order_tableController
 * @Package com.hdit.shop.controller
 * @Description 订单表的控制层类
 * @date 2021/1/19 14:42
 */
@Controller
@ResponseBody
public class Order_tableController {
    @Autowired
    private  Order_tableService order_tableService;
    @Autowired
    private Order_itemService orderItemService;
    @Autowired
    private CartService cartService;

    /**
     * 方法描述: 生成订单和对应数据处理
     * @author Fhy
     * @Date 2021/1/19 20:34
     * @params [order_table]
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @RequestMapping("/addOrder")
    public RestDate addOrder(@RequestBody Order_table order_table) {
        RestDate restDate = order_tableService.addOrderAll(order_table);
        return restDate;
    }

    /**
     * 方法描述: 查找所有订单，并分页
     * @author Fhy
     * @Date 2021/1/20 10:56
     * @params [map]
     * @return java.util.Map
     */
    @RequestMapping("/findAllOrder")
    public Map findAllOrder(@RequestBody Map<String,Integer> map) {
//        查询订单总数量（用于前端页码的计算）
        int pageCount = order_tableService.findAllOrderPaging();
        System.out.println("订单总数" + pageCount);
//        计算分页的起始数
        int start = (map.get("pageNum") -1 ) * map.get("pageSize");
        map.put("start",start);
//        查询当前参数所需的 订单数据
        List<Order_table> order_tableList = order_tableService.findAllOrder(map);


        Map callMap =  new HashMap();
        callMap.put("pageCount",pageCount);
        callMap.put("order_tableList",order_tableList);
        return callMap;
    }

    @RequestMapping("/findAll")
    public PageInfo<Order_table> findAll(int pageNum, int pageSize) {
        PageInfo<Order_table> pageInfoList = order_tableService.findAll(pageNum, pageSize);

        return pageInfoList;
    }

    /**
     * 方法描述: 通过用户id查找其所有订单
     * @author Fhy
     * @Date 2021/1/21 20:13
     * @params [map]
     * @return java.util.Map
     */
    @RequestMapping("/findOrderByUserId")
    public Map findOrderByUserId(@RequestBody Map<String,Integer> map) {
//        分页统计
        int pageCount = order_tableService.findOrderByUserIdPaging(map.get("order_user_id"));
//        分页数据
        int start = (map.get("pageNum") -1 ) * map.get("pageSize");
        map.put("start",start);
        List<Order_table> order_tableList = order_tableService.findOrderByUserId(map);
//        System.out.println("**************************************************************************************");
//        System.err.println(order_tableList);

//        返回数据
        Map callMap =  new HashMap();
        callMap.put("pageCount",pageCount);
        callMap.put("order_tableList",order_tableList);
        return callMap;
    }

    /**
     * 方法描述: 删除订单
     * @author Fhy
     * @Date 2021/1/20 14:49
     * @params [map]
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @RequestMapping("/deleteOrderById")
    public Map<String,String> deleteOrderById(@RequestBody Map<String,String> map) {
        String order_id = map.get("order_id");
        String mess = order_tableService.deleteOrderById(order_id);
        Map<String,String> message = new HashMap<>();
        message.put("message",mess);
        return message;
    }

    /**
     * 方法描述: 修改订单状态
     * @author Fhy
     * @Date 2021/1/21 20:14
     * @params [map]
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @RequestMapping("/updateOrderStatus")
    public Map<String,String> updateOrderStatus(@RequestBody Map<String,String> map) {
        String order_id = map.get("order_id");
        int order_status = Integer.parseInt(map.get("order_status"));
        String mess = order_tableService.updateOrderStatus(order_id,order_status);
        Map<String,String> message = new HashMap<>();
        message.put("message",mess);
        return message;
    }

    /***
     * 方法描述: 查找订单详情
     * @author Fhy
     * @Date 2021/1/25 10:29
     * @params [map]
     * @return com.hidt.shop.domain.Order_table
     */
    @RequestMapping("/findOrderDetail")
    public Order_table findOrderDetail(@RequestBody Map<String,String> map) {
        String order_id = map.get("order_id");
        Order_table order_table = order_tableService.findOrderDetail(order_id);
        return order_table;
    }
}
//方法1
//        Map<String, String[]> parameterMap = req.getParameterMap();
//        System.out.println(parameterMap.size());
//        Set<String> stringSet = parameterMap.keySet();
//        System.out.println("getObject");
//        Order_table order_table = null;
//        for (String  key:stringSet){
//            System.out.println(key);
//            ObjectMapper mapper=new ObjectMapper();
//            order_table = mapper.readValue(key,Order_table.class);
//            break;
//        }

//方法2
//        System.out.println(paramMap.size());
//        创建Order_table对象，并赋值
//        Order_table order_table = new Order_table();
//        order_table.setOrder_id(Integer.parseInt(paramMap.get("order_id")[0]));
//        order_table.setOrder_address(paramMap.get("order_address")[0]);
//        order_table.setOrder_user_id(Integer.parseInt(paramMap.get("order_user_id")[0]));
//        order_table.setOrder_sum_price(Integer.parseInt(paramMap.get("order_sum_price")[0]));
//        转换时间格式
//        order_table.setOrder_date(DateConverter.convert(paramMap.get("order_date")[0]));
//        order_table.setOrder_status(Integer.parseInt(paramMap.get("order_status")[0]));
//        order_table.setPayment_id(Integer.parseInt(paramMap.get("payment_id")[0]));
//        获取所有商品
//        String[] goods = paramMap.get("goods");
//        System.out.println(goods.length);
//        for (int i = 0; i < goods.length; i++) {
//            System.out.println(goods[i]);
//        }
//        String[] goodsId = paramMap.get("goodsId").split(",");
//        String[] goodsCount = paramMap.get("goodsCount").split(",");
//        for (int i = 0; i< goodsId.length; i++) {
//            System.out.println(goodsId[i]);
//            System.out.println(goodsCount[i]);
//        }